<template>
  <div class="main-menu">
    <!-- 头部标题 -->
    <div class="logo">
      <img src="@/assets/img/avantar.ico" alt="" class="img" />
      <h3 class="title" v-show="!isFold">后台管理系统</h3>
    </div>
    <!-- 菜单部分 -->
    <div class="menu">
      <el-menu
        :default-active="defaultActive"
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
        :collapse="isFold"
      >
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 字符串: el-icon-monitor => 组件 component动态组件 -->
              <!-- 在通过字符串来截取需要的数据 -->
              <el-icon>
                <component :is="item.icon.split('-icon-')[1]" />
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="itemchild in item.children" :key="itemchild.id">
              <el-menu-item :index="itemchild.id + ''" @click="handleItemClick(itemchild)">{{
                itemchild.name
              }}</el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>

      <!-- <el-menu
        default-active="3"
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
      >
        <el-sub-menu index="1">
          <template #title>
            <el-icon><location /></el-icon>
            <span>系统总览</span>
          </template>
          <el-menu-item> 核心技术 </el-menu-item>
          <el-menu-item> 商品统计 </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="2">
          <template #title>
            <el-icon><location /></el-icon>
            <span>系统管理</span>
          </template>
          <el-menu-item> 用户管理 </el-menu-item>
          <el-menu-item> 部门管理 </el-menu-item>
          <el-menu-item> 菜单管理 </el-menu-item>
          <el-menu-item> 角色管理 </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="3">
          <template #title>
            <el-icon><location /></el-icon>
            <span>商品中心</span>
          </template>
          <el-menu-item> 商品类别 </el-menu-item>
          <el-menu-item> 商品信息 </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="4">
          <template #title>
            <el-icon><location /></el-icon>
            <span>随便聊聊</span>
          </template>
          <el-menu-item> 你的故事 </el-menu-item>
          <el-menu-item> 故事列表 </el-menu-item>
          <el-sub-menu index="5">
            <template #title>
              <span>喜欢的音乐</span>
            </template>
            <el-menu-item>音乐列表</el-menu-item>
            <el-menu-item>音乐介绍</el-menu-item>
          </el-sub-menu>
        </el-sub-menu>
      </el-menu> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import useLoginAccountStore from '@/store/login/login'
import { useRoute, useRouter } from 'vue-router'
import { mapPathToMenu } from '@/utils/map-menus'
import { computed } from 'vue'

// 获取动态菜单
const loginAccountStore = useLoginAccountStore()
const userMenus = loginAccountStore.userMenus

// 接收父组件传来的参数
defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})

// 监听item的点击
const router = useRouter()
function handleItemClick(item: any) {
  const url = item.url
  router.push(url)
}

// 根据当前路径来显示菜单
const route = useRoute()
const defaultActive = computed(() => {
  const pathMenu = mapPathToMenu(route.path, userMenus)
  return pathMenu?.id + ''
})
</script>

<style lang="less" scoped>
.main-menu {
  .logo {
    height: 60px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    h3 {
      white-space: nowrap;
    }
    .img {
      width: 25px;
      height: 25px;
    }
    .title {
      color: #fff;
      margin-left: 8px;
    }
  }
  .menu {
    .el-menu {
      user-select: none;
      border-right: none;
      .el-sub-menu {
        background-color: #01132c;
        // padding-left: 20px;

        // 0A1C35
        .el-menu-item {
          background-color: #0a1c35;
          color: #adb8bc;
        }

        .el-icon,
        span {
          color: #adb8bc;
        }
        .el-menu-item:hover {
          color: #fff;
        }
        .el-menu-item.is-active {
          background-color: #bbb;
          color: #fff;
        }
      }
    }
  }
}
</style>
